package sg.edu.nus.iss.vmcs.system;

import java.util.HashMap;
import sg.edu.nus.iss.vmcs.abstraction.IMediator;
import sg.edu.nus.iss.vmcs.abstraction.PresenterBase;
/*
 * @author nyi nyi than
 */
public class Mediator implements IMediator
{
	private HashMap<String, PresenterBase<?>> presenters = new HashMap<String, PresenterBase<?>>();

	@Override
	public void deregister(PresenterBase<?> presenter)
	{
		deregister(presenter.getName());
	}

	@Override
	public void deregister(String presenterName)
	{
		if (this.presenters.containsKey(presenterName))
		{
			this.presenters.remove(presenterName);
		}
	}

	@Override
	public void register(PresenterBase<?> presenter)
	{
		if (!this.presenters.containsKey(presenter.getName()))
		{
			this.presenters.put(presenter.getName(), presenter);						
		}
	}

	@Override
	public void sendMessage(PresenterBase<?> sender, String receiverName,
			String message)
	{
		PresenterBase<?> receiver = this.presenters.get(receiverName);
		if (receiver != null)
		{
			receiver.notify(sender, message);
		}
	}

}
